package com.dangf.app.launcher.dialog

import android.content.Context
import android.media.AudioManager
import android.provider.Settings
import android.view.KeyEvent
import android.view.View
import android.widget.SeekBar
import com.dangf.app.launcher.R
import pw.hais.utils_lib.app.AppBaseActivity
import pw.hais.utils_lib.app.BaseDialog


/**
 * Created by hais1992 on 2017/9/9/009.
 */
class SystemSettingDialog(activity: AppBaseActivity) : BaseDialog(activity, R.layout.windows_system_setting), SeekBar.OnSeekBarChangeListener {
    private val mAudioManager by lazy { activity.getSystemService(Context.AUDIO_SERVICE) as AudioManager }
    private var seekbar_liangdu: SeekBar? = null
    private var seekbar_yinliang: SeekBar? = null

    override fun initView(view: View) {
        seekbar_liangdu = view.findViewById(R.id.seekbar_liangdu)
        seekbar_yinliang = view.findViewById(R.id.seekbar_yinliang)

        seekbar_liangdu?.setOnSeekBarChangeListener(this)
        seekbar_yinliang?.setOnSeekBarChangeListener(this)

        seekbar_liangdu?.progress = Settings.System.getInt(activity.contentResolver, Settings.System.SCREEN_BRIGHTNESS)
        seekbar_yinliang?.progress = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
        seekbar_yinliang?.max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)

        downTime()
    }

    override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
        upKeyTime = System.currentTimeMillis()
        when (p0?.id) {
            R.id.seekbar_liangdu -> {
                Settings.System.putInt(activity.contentResolver, Settings.System.SCREEN_BRIGHTNESS, p1)
            }
            R.id.seekbar_yinliang -> {
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, p1, AudioManager.FLAG_PLAY_SOUND or AudioManager.FLAG_SHOW_UI)
            }
        }
    }

    override fun onStartTrackingTouch(p0: SeekBar?) {

    }

    override fun onStopTrackingTouch(p0: SeekBar?) {

    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        upKeyTime = System.currentTimeMillis()
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            mAudioManager.adjustStreamVolume(
                    AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_RAISE,
                    AudioManager.FLAG_PLAY_SOUND or AudioManager.FLAG_SHOW_UI)

            seekbar_yinliang?.progress = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
            return true
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            mAudioManager.adjustStreamVolume(
                    AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_LOWER,
                    AudioManager.FLAG_PLAY_SOUND or AudioManager.FLAG_SHOW_UI)

            seekbar_yinliang?.progress = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
            return true
        }
        return super.onKeyDown(keyCode, event)
    }


    private var upKeyTime = System.currentTimeMillis()
    private fun downTime() {
        if(System.currentTimeMillis() - upKeyTime >20*1000)dismiss()
        else seekbar_liangdu?.postDelayed({ downTime() }, 1000)
    }

}